package com.huichenghe.xinlvsh01.Ble;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.huichenghe.xinlvsh01.DataEntites.OutLineDataEntity;
import com.huichenghe.xinlvsh01.DbEntities.MyDBHelperForDayData;
import com.huichenghe.xinlvsh01.Utils.FormatUtils;
import com.huichenghe.xinlvsh01.Utils.LocalDataSaveTool;
import com.huichenghe.xinlvsh01.Utils.MyConfingInfo;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 处理离线数据的帮助类
 * Created by lixiaoning on 15-11-17.
 */
public class BleBaseDataForOutlineMovement extends BleBaseDataManage
{
    public static final String TAG = BleDataForDayData.class.getSimpleName();
    public static final byte mNotify_cmd = (byte)0xa2;
    public static final byte toDevice = (byte)0x22;


    //68 a2 1100 8b436d38 9d436d38 01 0000000000000000 1416
    public void dealTheData(Context mContext, byte[] data, int length)
    {
//      15665c56 16675c56 01 04000000 2c000000 a816
        Log.i(TAG, "解析手环端上传的离线数据" + FormatUtils.bytesToHexString(data));
        TimeZone tz = TimeZone.getDefault();        // 获取默认时区
        int beginT = FormatUtils.byte2Int(data, 0) - tz.getRawOffset()/1000;
        int endT = FormatUtils.byte2Int(data, 4) - tz.getRawOffset()/1000;
        long b = (long)beginT;
        long e = (long)endT;

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date d = new Date(b * 1000);
        String day = format.format(d);
        Log.i(TAG, "开始时间------：" + day + "int :" + beginT + endT);




        // 开始时间
        String beginTime = splitTime(b * 1000);
        // 结束时间
        String endTime = splitTime(e * 1000);

        byte pak =  data[8];

        if(1 == pak)        // 表示心率总数据
        {
            boolean isHas = false;
            int calorie = FormatUtils.byte2Int(data, 9);
            int stepTotle = FormatUtils.byte2Int(data, 13);
            Log.i(TAG, "离线数据卡路里：" + calorie + "计步：" + stepTotle);

            String userAccount = LocalDataSaveTool.getInstance(mContext).readSp(MyConfingInfo.USER_ACCOUNT);

            // 先查询数据库，判断是否是同一条数据重复上传，是则忽略，步数则添加

            Cursor cursor = MyDBHelperForDayData.getInstance(mContext).selectOutLineData(mContext, userAccount, day);
            if(cursor.getCount() != 0)
            {
                isHas = judgeTime(beginTime, endTime, cursor);
                Log.i(TAG, "查询数据库数据" + isHas);
            }

            if(!isHas)
            {
                String type = String.valueOf(OutLineDataEntity.TYPE_UNKNOW);
                Log.i(TAG, "查询数据库有数据无重复数据，可以添加");
                // 将数据存入数据库
                MyDBHelperForDayData dbHelperForDayData = MyDBHelperForDayData.getInstance(mContext);
                dbHelperForDayData.insertOutLineData(mContext, userAccount, type, day, beginTime, endTime, calorie, stepTotle);
            }
        }else if(2 == pak)
        {

        }else if(3 == pak)
        {

        }


        // 发送响应数据
        byte[] back = new byte[9];
        for (int i = 0; i < back.length; i ++)
        {
            back[i] = data[i];
        }


        setMsgToByteDataAndSendToDevice(toDevice, back, back.length, true);

    }


    /**
     * 比较开始时间和结束时间，正常情况下不可能相同
     * @param beginTime
     * @param endTime
     */
    private boolean judgeTime(String beginTime, String endTime, Cursor cursor)
    {
        boolean isHas = false;

        if(cursor.moveToFirst())
        {
            do {
                String start =  cursor.getString(cursor.getColumnIndex("startTime"));
                String end = cursor.getString(cursor.getColumnIndex("endTime"));

                Log.i(TAG, "数据库离线数据：" + start + "--" + end);

                if(beginTime.equals(start) && endTime.equals(end))
                {
                    isHas = true;
                }

            }while (cursor.moveToNext());
        }


        return isHas;
    }


    public String splitTime(long mi)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(mi);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        return hour + ":" + minute;
    }


}
